package com.it.chatgptchen.service.impl;



import com.it.chatgptchen.chatgpt.OpenAiClient;
import com.it.chatgptchen.chatgpt.entity.chat.ChatCompletion;
import com.it.chatgptchen.chatgpt.entity.chat.ChatCompletionResponse;
import com.it.chatgptchen.chatgpt.entity.chat.Message;
import com.it.chatgptchen.domain.ProStateManagement;
import com.it.chatgptchen.domain.common.AjaxResultBox;
import com.it.chatgptchen.service.IProProgressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * Service业务层处理
 *
 * @date 2023-04-24
 */
@Service
public class ProProgressServiceImpl implements IProProgressService {


    @Autowired
    private  OpenAiClient openAiClient;



    @Override
    public AjaxResultBox sseChat(ProStateManagement proStateManagement) {
        List<String> ss=new ArrayList<>();
        //聊天模型：gpt-3.5
        Message message = Message.builder().role(Message.Role.USER).content(proStateManagement.getText()).build();
        ChatCompletion chatCompletion = ChatCompletion
                .builder()
                .messages(Arrays.asList(message))
                .model(ChatCompletion.Model.GPT_3_5_TURBO.getName())
                .build();
        ChatCompletionResponse chatCompletionResponse = openAiClient.chatCompletion(chatCompletion);
        chatCompletionResponse.getChoices().forEach(e -> {
            ss.add(e.getMessage().getContent());
            System.out.println(e.getMessage());
        });
        return AjaxResultBox.success(ss);
    }

}
